import Vue from 'vue'
import VueRouter from 'vue-router'

import login from '@/components/login.vue'
import registered from '@/components/registered.vue'
import userinfo from '@/components/userinfo.vue'
import userdit from '@/components/userdit.vue'
import attention from '@/components/attention.vue'
// import text from '@/components/text.vue'
import text from '@/components/TestCommentDiguiPage.vue'
import home from '@/views/Home.vue'
import collect from '@/components/collect.vue'
import detail from '@/components/detail.vue'
import comment from '@/components/comment.vue'
import usecomment from '@/components/usercomment.vue'
import search from '@/components/search.vue'
import mange from '@/components/mange.vue'
import { getToken } from '@/untails/mytoken'
Vue.use(VueRouter)

const routes = [
  {
    path:'/home',
    name:'home',
    component:home
  },
  {
    path:'/mange',
    name:'mange',
    component:mange
  },
  {
    path:'/search',
    name:'search',
    component:search
  },
  {
    path:'/comment/:id',
    name:'comment',
    component:comment
  },
  {
    path:'/detail/:id',
    name:'detail',
    component:detail
  },
  {
    path:'/collect',
    name:'collect',
    component:collect
  },
  {
    path:'/text',
    name:'text',
    component:text
  },
  {
    path:'/attention',
    name:'attention',
    component:attention
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/registered',
    name: 'registered',
    component: registered
  },
  {
    path:'/userinfo',
    name:'userinfo',
    component:userinfo,
    meta: { 
      use:true
     }
  },
  {
    path:'/usecomment',
    name:'usecomment',
    component:usecomment,
    meta: { 
      use:true
     }
  },
  {
    path:'/userdit',
    name:'userdit',
    component:userdit,
    meta: { 
      use:true
     }
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if(to.meta.use===true){
    if(!getToken()){
      return router.replace('/login')
    }
  }
  next()
})
export default router
